package manage.handler;

import manage.entity.StudentDo;
import manage.service.StudentService;
import manage.service.impl.StudentServiceImpl;
import manage.student.MainView;
import manage.student.view.AddStudentView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddStudentViewHandler implements ActionListener {
    private AddStudentView addStudentView;
    private MainView mainView;
    public AddStudentViewHandler(AddStudentView addStudentView, MainView mainView){
        this.addStudentView=addStudentView;
        this.mainView=mainView;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton=(JButton) e.getSource();
        String text=jButton.getText();
        if("添加".equals(text)){
            StudentService studentService=new StudentServiceImpl();
            StudentDo studentDo=addStudentView.buildStudent();
            boolean addResult=studentService.add(studentDo);
            if(addResult){
                //更新表格数据
                mainView.reloadTable();
                JOptionPane.showMessageDialog(addStudentView,"添加成功");
                addStudentView.dispose();
            }else{
                JOptionPane.showMessageDialog(addStudentView,"添加失败");
            }
        }
    }
}
